<?php
// 修复物料编码和分类数据

try {
    // 直接连接数据库
    $pdo = new PDO('mysql:host=127.0.0.1;port=3306;dbname=scanwork', 'scanwork', 'rDezxK9h7tWp5sP7');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "开始修复物料数据...\n";
    
    // 面料类 (category_id = 1)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 1, code = 'MAT001' WHERE name = '高档沙发布' AND id = 1");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 1, code = 'MAT006' WHERE name = '真皮'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 1, code = 'MAT007' WHERE name = 'PU皮'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 1, code = 'MAT008' WHERE name = '布艺面料'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 1, code = 'MAT009' WHERE name = '高档沙发布' AND id > 1");
    
    // 填充材料类 (category_id = 2)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT004' WHERE name = '海绵' AND id = 4");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT010' WHERE name = '高密度海绵'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT011' WHERE name = '中密度海绵'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT012' WHERE name = '低密度海绵'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT013' WHERE name = '黑心棉'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT014' WHERE name = '珍珠棉'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT015' WHERE name = '喷胶棉'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 2, code = 'MAT016' WHERE name = '无纺布'");
    
    // 弹簧类 (category_id = 3)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 3, code = 'MAT017' WHERE name = '蛇形弹簧'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 3, code = 'MAT018' WHERE name = '拉簧'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 3, code = 'MAT019' WHERE name = '弹簧扣'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 3, code = 'MAT020' WHERE name = '弹簧钢丝'");
    
    // 木工材料类 (category_id = 4)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT021' WHERE name = '松木方4*6'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT022' WHERE name = '松木方3*5'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT023' WHERE name = '松木方2*4'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT024' WHERE name = '松木方5*8'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT025' WHERE name = '松木方6*10'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT026' WHERE name = '多层板0.7'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT027' WHERE name = '多层板1.5'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT028' WHERE name = '密度板0.7'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT029' WHERE name = '密度板1.5'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT030' WHERE name = '刨花板0.7'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 4, code = 'MAT031' WHERE name = '刨花板1.5'");
    
    // 五金配件类 (category_id = 5)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT003' WHERE name = '沙发脚'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT032' WHERE name = '螺丝M6*20'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT033' WHERE name = '螺母M6'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT034' WHERE name = '垫片M6'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT035' WHERE name = '角码'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT036' WHERE name = '连接板'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT037' WHERE name = '扪皮钉'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 5, code = 'MAT038' WHERE name = '打底钉'");
    
    // 缝纫用品类 (category_id = 6)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT002' WHERE name = '拉链' AND id = 2");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT005' WHERE name = '缝纫线' AND id = 5");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT039' WHERE name = '缝纫线402白'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT040' WHERE name = '缝纫线402黑'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT041' WHERE name = '缝纫线402米'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT042' WHERE name = '缝纫针14#'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT043' WHERE name = '缝纫针16#'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT044' WHERE name = '压脚'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT045' WHERE name = '梭芯'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT046' WHERE name = '梭壳'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT047' WHERE name = '拉链' AND id != 2");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT048' WHERE name = '拉链头'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT049' WHERE name = '拉链布带'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT050' WHERE name = '包边条'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT051' WHERE name = '魔术贴'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 6, code = 'MAT052' WHERE name = '装饰扣'");
    
    // 胶水类 (category_id = 7)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 7, code = 'MAT053' WHERE name = '扪皮胶'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 7, code = 'MAT054' WHERE name = '海绵胶'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 7, code = 'MAT055' WHERE name = '热熔胶'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 7, code = 'MAT056' WHERE name = '开棉胶'");
    $db->execute("UPDATE fa_scanwork_material SET category_id = 7, code = 'MAT057' WHERE name = '贴棉胶带'");
    
    // 工具类 (category_id = 8)
    $db->execute("UPDATE fa_scanwork_material SET category_id = 8, code = 'MAT058' WHERE name = '开棉刀片'");
    
    echo "物料数据修复完成！\n";
    
    // 验证结果
    $result = $db->query("SELECT COUNT(*) as total, COUNT(code) as has_code, COUNT(category_id) as has_category FROM fa_scanwork_material");
    echo "总物料数: " . $result[0]['total'] . "\n";
    echo "有编码的物料: " . $result[0]['has_code'] . "\n";
    echo "有分类的物料: " . $result[0]['has_category'] . "\n";
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}
